#ifndef TCOMMANDEXECPROCESS_H
#define TCOMMANDEXECPROCESS_H

#include <QObject>
#include <QList>
#include <sshconnection.h>
#include <sshremoteprocess.h>
#include <sftpchannel.h>
#include <QTimer>
#include <windows.h>

const QString CMD_TYPE_CHECK = "LR_UP_DAT_PROJ";
const QString CMD_TYPE_PLAIN_COMMAND = "PlainCommand";
const QString CMD_TYPE_UPLOAD_FILE = "UploadFile";

const int LRWM_UPDATPLUGIN_CONNECTOK = WM_USER + 1;
const int LRWM_UPDATPLUGIN_FINISH = WM_USER + 2;
const int LRWM_UPDATPLUGIN_FAIL = WM_USER + 3;
const int LRWM_UPDATPLUGIN_KEEPCONNECT = WM_USER + 4;
const int LRWM_UPDATPLUGIN_EXIT = WM_USER + 5;

//指令单元
struct TCommandUnit {
    QString Type;         //类型
    QString Command;      //指令
    QString FileSrcPath;  //源文件路径
    QString FileDstPath;  //目标文件路径 设备中的路径
};

class TCommandExecProcess : public QObject
{
    Q_OBJECT
public:
    explicit TCommandExecProcess(QObject *parent = nullptr);
    ~TCommandExecProcess();

    //设置参数
    void setConfig(const QString &configFilePath,const QString &devIP);

private:
    //指令
    QList<TCommandUnit> m_commandUnitList;
    int m_commandIndex;
    QByteArray m_commandReply;
    //ssh参数
    QSsh::SshConnectionParameters m_sshParams;
    QSsh::SshConnection *m_sshConnection;
    QSsh::SshRemoteProcess::Ptr m_sshShell;
    QSsh::SftpChannel::Ptr m_sftpChannel;
    bool m_shellReady;
    bool m_sftpReady;
    //升级文件信息
    QString m_configFilePath;
    //目标设备IP
    QString m_devIP;
    //升级是否成功
    bool m_upDatFinish;

    //从文件中加载
    void loadFromFile(const QString &filePath);
    //连接设备
    void connectDevice(const QString &ipAddr);
    //执行指令
    void executeCommand();

public slots:
    //开始升级
    void slotStartUpDat();

private slots:
    //ssh连接信号
    void slotSshConnected();
    void slotSshDisconnected();
    void slotSshError(QSsh::SshError error);
    //ssh shell信号
    void slotSshShellStarted();
    void slotSshShellReadyReadStandardOutput();
    void slotSshShellReadyReadStandardError();
    void slotSshShellClosed();
    //ssh sftp信号
    void slotSftpInitialized();
    void slotSftpClosed();
    void slotSftpFinished(QSsh::SftpJobId job,const QString &error);

signals:
    //升级消息
    void sigUpdatMessage(int message,bool exitApp);
    //打印日志
    void sigShowLog(const QString &logStr);

};

#endif // TCOMMANDEXECPROCESS_H
